package gui;

import ge.escola.Disciplina;
import ge.escola.Serie;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AlteraSerie extends javax.swing.JPanel {
	private JLabel jLabel1;
	private JTextArea textArea;
	private JLabel jLabel2;
	private JTextField caixaNomeSerie;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JButton buttonAlterar;
	private JButton jButton1;
	private JLabel jLabel6;
	private JButton buttonRemover;
	private JButton buttonAdicionar;
	private JTextField caixaCodigo;

	private Serie serie;
	private Disciplina disciplina;
	private boolean lock = false;
	private String codigoDisciplina;
	
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	
	public AlteraSerie() {
		super();
		initGUI();
		
		serie = SelecionaSerie.serie;
		caixaNomeSerie.setText(serie.getNome());
		caixaNomeSerie.setEditable(false);
		
		atualizaDisciplinas();
		
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(400, 300));
			this.setSize(800, 600);
			this.setBackground(new java.awt.Color(204,204,255));
			this.setLayout(null);
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Alterar serie");
				jLabel1.setBounds(221, 29, 331, 85);
				jLabel1.setForeground(new java.awt.Color(0,102,102));
				jLabel1.setFont(new java.awt.Font("Arial",0,48));
				jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
				jLabel1.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
			}
			{
				textArea = new JTextArea();
				this.add(textArea);
				textArea.setBounds(437, 184, 296, 391);
				textArea.setFont(new java.awt.Font("Arial",0,12));
				textArea.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setText("Serie");
				jLabel2.setBounds(87, 171, 61, 34);
				jLabel2.setFont(new java.awt.Font("Arial",0,24));
				jLabel2.setForeground(new java.awt.Color(0,102,102));
			}
			{
				caixaNomeSerie = new JTextField();
				this.add(caixaNomeSerie);
				caixaNomeSerie.setBounds(158, 178, 122, 29);
				caixaNomeSerie.setFont(new java.awt.Font("Arial",0,16));
			}
			{
				jLabel3 = new JLabel();
				this.add(jLabel3);
				jLabel3.setText("Adicionar/Remover disciplinas da serie");
				jLabel3.setBounds(30, 327, 353, 40);
				jLabel3.setForeground(new java.awt.Color(0,102,102));
				jLabel3.setFont(new java.awt.Font("Arial",0,18));
				jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
				jLabel3.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
			}
			{
				jLabel4 = new JLabel();
				this.add(jLabel4);
				jLabel4.setText("Disciplinas que ja estao na serie");
				jLabel4.setBounds(474, 152, 222, 26);
				jLabel4.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				jLabel4.setFont(new java.awt.Font("Arial",0,14));
				jLabel4.setForeground(new java.awt.Color(0,102,102));
				jLabel4.setHorizontalAlignment(SwingConstants.CENTER);
			}
			{
				jLabel5 = new JLabel();
				this.add(jLabel5);
				jLabel5.setText("Codigo da disciplina");
				jLabel5.setHorizontalAlignment(SwingConstants.CENTER);
				jLabel5.setFont(new java.awt.Font("Arial",0,14));
				jLabel5.setForeground(new java.awt.Color(0,102,102));
				jLabel5.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				jLabel5.setBounds(122, 400, 158, 26);
			}
			{
				caixaCodigo = new JTextField();
				this.add(caixaCodigo);
				caixaCodigo.setFont(new java.awt.Font("Arial",0,16));
				caixaCodigo.setBounds(134, 438, 132, 29);
			}
			{
				buttonAdicionar = new JButton();
				this.add(buttonAdicionar);
				buttonAdicionar.setText("Adicionar");
				buttonAdicionar.setBounds(238, 484, 93, 29);
				buttonAdicionar.setFont(new java.awt.Font("Arial",0,14));
				buttonAdicionar.setBackground(new java.awt.Color(255,255,255));
				buttonAdicionar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonAdicionarMouseClicked(evt);
					}
				});
			}
			{
				buttonRemover = new JButton();
				this.add(buttonRemover);
				buttonRemover.setText("Remover");
				buttonRemover.setBackground(new java.awt.Color(255,255,255));
				buttonRemover.setFont(new java.awt.Font("Arial",0,14));
				buttonRemover.setBounds(75, 484, 93, 29);
				buttonRemover.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonRemoverMouseClicked(evt);
					}
				});
			}
			{
				jButton1 = new JButton();
				this.add(jButton1);
				jButton1.setText("VOLTAR");
				jButton1.setBackground(new java.awt.Color(255,255,255));
				jButton1.setFont(new java.awt.Font("Arial",0,14));
				jButton1.setBounds(156, 546, 93, 29);
			}
			{
				jLabel6 = new JLabel();
				this.add(jLabel6);
				jLabel6.setBounds(61, 161, 269, 60);
				jLabel6.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
			}
			{
				buttonAlterar = new JButton();
				this.add(buttonAlterar);
				buttonAlterar.setText("Alterar");
				buttonAlterar.setBounds(342, 181, 61, 23);
				buttonAlterar.setBackground(new java.awt.Color(255,255,255));
				buttonAlterar.setFont(new java.awt.Font("Arial",0,12));
				buttonAlterar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonAlterarMouseClicked(evt);
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void buttonAlterarMouseClicked(MouseEvent evt) {
		if(lock){
			caixaNomeSerie.setEditable(false);
			buttonAlterar.setText("Alterar");
			lock = false;
		}else{
			caixaNomeSerie.setEditable(true);
			buttonAlterar.setText("OK");
			lock = true;
		}
	}
	
	private void buttonRemoverMouseClicked(MouseEvent evt) {
		codigoDisciplina = caixaCodigo.getText();
		disciplina = capturaDisciplina(codigoDisciplina);
		if(!codigoDisciplina.equals("") && disciplina != null){
			try {
				serie.removeDisciplina(disciplina);
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null, "Essa serie nao possui essa disciplina para que ela seja removida.");
			}finally{
				atualizaDisciplinas();
			}
		}else{
			JOptionPane.showMessageDialog(null, "Disciplina nao encontrada.");
		}
	}
	
	private void buttonAdicionarMouseClicked(MouseEvent evt) {
		codigoDisciplina = caixaCodigo.getText();
		disciplina = capturaDisciplina(codigoDisciplina);
		if(!codigoDisciplina.equals("") && disciplina != null){
			try {
				serie.addDisciplina(disciplina);
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null, "Essa serie ja possui essa disciplina.");
			}finally{
				atualizaDisciplinas();
			}
		}else{
			JOptionPane.showMessageDialog(null, "Disciplina nao encontrada.");
		}
	}
	
	private Disciplina capturaDisciplina(String codigo){
		for(Disciplina disciplina2:GerenciadorDeJanelas.todasDisciplinas){
			if(disciplina2.getCodigo().equals(codigo)){
				return disciplina2;
			}
		}
		return null;
	}
	
	private void atualizaDisciplinas(){
		String saida = "";
		if(serie.getDisciplinasSerie().size() <= 0){
			saida = "Nenhuma disciplina cadastrada.";
		}else{
			for(Disciplina disciplina:GerenciadorDeJanelas.todasDisciplinas){
				saida += disciplina.toString() + "\n";
			}
		}
		textArea.setEditable(true);
		textArea.setText(saida);
		textArea.setEditable(false);
	}
}
